
#include <BLabel>
#include <BGridLayout>
#include <BVBoxLayout>
#include <BHBoxLayout>
#include <BGroupBox>

#include "StringUtils.h"
#include "GearRestraintPanel.h"

using namespace BWE;

GearRestraintPanel::GearRestraintPanel(BWidget* parent) : BWidget(parent)
{
	this->setTitle(BGearRestraint::TypeName());

	_bodyAGroupBox = new BGroupBox();
	{
		_positionAEdit = new BLineEdit();
		_rotateAEdit = new BLineEdit();
		BGridLayout* gridLayout = new BGridLayout(_bodyAGroupBox);
		gridLayout->addWidget(0, 0, new BLabel("position"));
		gridLayout->addWidget(0, 1, _positionAEdit);
		gridLayout->addWidget(1, 0, new BLabel("rotate"));
		gridLayout->addWidget(1, 1, _rotateAEdit);
	}

	_bodyBGroupBox = new BGroupBox();
	{
		_positionBEdit = new BLineEdit();
		_rotateBEdit = new BLineEdit();
		BGridLayout* gridLayout = new BGridLayout(_bodyBGroupBox);
		gridLayout->addWidget(0, 0, new BLabel("position"));
		gridLayout->addWidget(0, 1, _positionBEdit);
		gridLayout->addWidget(1, 0, new BLabel("rotate"));
		gridLayout->addWidget(1, 1, _rotateBEdit);
	}

	BGroupBox* linearLimitGroupBox = new BGroupBox("Linear Limit");
	{
		BLabel* ratioLabel = new BLabel("ratio");
		_ratioSpinBox = new BRealSpinBox();

		BGridLayout* gridLayout = new BGridLayout(linearLimitGroupBox);
		gridLayout->addWidget(0, 0, ratioLabel);
		gridLayout->addWidget(0, 1, _ratioSpinBox);
	}

	BVBoxLayout* vlayout = new BVBoxLayout(this);
	vlayout->addWidget(_bodyAGroupBox);
	vlayout->addWidget(_bodyBGroupBox);
	vlayout->addWidget(linearLimitGroupBox);

	connect(this, Signal_AssetChanged, &GearRestraintPanel::slotAssetChanged);

	connect(_positionAEdit, Signal_EditFinished, &GearRestraintPanel::slotPositionAEditFinished);
	connect(_rotateAEdit, Signal_EditFinished, &GearRestraintPanel::slotRotateAEditFinished);
	connect(_positionBEdit, Signal_EditFinished, &GearRestraintPanel::slotPositionBEditFinished);
	connect(_rotateBEdit, Signal_EditFinished, &GearRestraintPanel::slotRotateBEditFinished);

	connect(_ratioSpinBox, Signal_EditFinished, &GearRestraintPanel::slotRatioEditFinished);
}
GearRestraintPanel::~GearRestraintPanel()
{

}

void GearRestraintPanel::slotAssetChanged(BObject* object, const BValue& value)
{
	_gearRestraint = value;
	if (_gearRestraint)
	{
		if (_gearRestraint->bodyA())
		{
			const BVector& pivotA = _gearRestraint->pivotA();
			_bodyAGroupBox->setTitle(_gearRestraint->bodyA()->name());
			_positionAEdit->setText(StringFromVector(pivotA));
			//_rotateAEdit->setText(StringFromQuater(matrixA.rotate()));
		}
		if (_gearRestraint->bodyB())
		{
			const BVector& pivotB = _gearRestraint->pivotB();
			_bodyBGroupBox->setTitle(_gearRestraint->bodyB()->name());
			_positionBEdit->setText(StringFromVector(pivotB));
			//_rotateBEdit->setText(StringFromQuater(matrixB.rotate()));
		}

		_ratioSpinBox->setValue(_gearRestraint->ratio());
	}
}

void GearRestraintPanel::slotPositionAEditFinished(BObject* object, const BValue& value)
{
	const BString& text = value;
	BVector pivotA = StringToVector(text);
	_gearRestraint->setPivotA(pivotA);
}
void GearRestraintPanel::slotRotateAEditFinished(BObject* object, const BValue& value)
{
	const BString& text = value;
	const BQuater& rotate = StringToQuater(text);
	//BMatrix matrixA = _gearRestraint->matrixA();
	//matrixA.setRotate(rotate);
	//_gearRestraint->setMatrixA(matrixA);
}
void GearRestraintPanel::slotPositionBEditFinished(BObject* object, const BValue& value)
{
	const BString& text = value;
	BVector pivotB = StringToVector(text);
	_gearRestraint->setPivotB(pivotB);
}
void GearRestraintPanel::slotRotateBEditFinished(BObject* object, const BValue& value)
{
	const BString& text = value;
	const BQuater& rotate = StringToQuater(text);
	//BMatrix matrixB = _gearRestraint->matrixB();
	//matrixB.setRotate(rotate);
	//_gearRestraint->setMatrixB(matrixB);
}

void GearRestraintPanel::slotRatioEditFinished(BObject* object, const BValue& value)
{
	BReal ratio = value;
	_gearRestraint->setRatio(ratio);
}
